var _util = _interopRequireDefault(require("../../public/utils/util"));

function _interopRequireDefault(t) {
    return t && t.__esModule ? t : {
        default: t
    };
}

function _slicedToArray(t, e) {
    return _arrayWithHoles(t) || _iterableToArrayLimit(t, e) || _nonIterableRest();
}

function _nonIterableRest() {
    throw new TypeError("Invalid attempt to destructure non-iterable instance");
}

function _iterableToArrayLimit(t, e) {
    var r = [], a = !0, n = !1, i = void 0;
    try {
        for (var u, o = t[Symbol.iterator](); !(a = (u = o.next()).done) && (r.push(u.value), 
        !e || r.length !== e); a = !0) ;
    } catch (t) {
        n = !0, i = t;
    } finally {
        try {
            a || null == o.return || o.return();
        } finally {
            if (n) throw i;
        }
    }
    return r;
}

function _arrayWithHoles(t) {
    if (Array.isArray(t)) return t;
}

Component({
    properties: {
        start: {
            type: String,
            value: "1990-01-01 00:00"
        },
        end: String,
        value: {
            type: String,
            value: ""
        },
        format: {
            type: String,
            value: "Y-M-D m:h"
        },
        isShown: {
            type: Boolean,
            value: !1,
            observer: function(t) {
                t && this.initData();
            }
        },
        minuteStep: {
            type: String,
            value: 1
        }
    },
    data: {
        newVal: [ 0, 0, 0, 0, 0 ],
        reset: !1
    },
    methods: {
        initData: function() {
            var t = this, e = _util.default.formatLocalDate(new Date(), "Y-M-D h:m"), r = this.data, a = r.value, n = r.start, i = r.minuteStep, u = this.splitDate(a || e), o = this.splitDate(n), s = 60 / i, l = {
                years: Array.from(Array(100), function(t, e) {
                    return +o[0] + e;
                }),
                months: Array.from(Array(12), function(t, e) {
                    return 1 + e;
                }),
                days: this.getDays(u[0], u[1]),
                hours: Array.from(Array(24), function(t, e) {
                    return _util.default.formatNumber(e);
                }),
                minutes: Array.from(Array(s), function(t, e) {
                    return _util.default.formatNumber(e * i);
                }),
                reset: !!a
            }, f = this.getDataInIndex(l, u);
            this.start = this.getDataInIndex(l, o), this.setData(l, function() {
                return t.setData({
                    newVal: f
                });
            });
        },
        getDataInIndex: function(r, a) {
            return [ "years", "months", "days", "hours", "minutes" ].map(function(t, e) {
                return r[t].indexOf(e < 3 ? +a[e] : a[e]);
            }).map(function(t) {
                return t < 0 ? 0 : t;
            });
        },
        getDays: function(t, e) {
            12 == +e ? t = +t + (e = 1) : e = +e + 1;
            var r = new Date("".concat(t, "/").concat(e, "/01"));
            r.setDate(r.getDate() - 1);
            var a = r.getDate();
            return Array.from(Array(a), function(t, e) {
                return 1 + e;
            });
        },
        splitDate: function(t) {
            return t.match(/\d+/g);
        },
        getDate: function(t) {
            var r = this.data, a = [ "years", "months", "days", "hours", "minutes" ];
            return t.map(function(t, e) {
                return r[a[e]][t];
            });
        },
        joinDate: function(t) {
            var e = this.data.format, r = _slicedToArray(t, 5), a = r[0], n = r[1], i = r[2], u = r[3], o = r[4];
            return e.replace("Y", a).replace("M", n).replace("D", i).replace("m", u).replace("h", o);
        },
        isOverStartTime: function(t) {
            var e = this.start.map(_util.default.formatNumber);
            return t = t.map(_util.default.formatNumber), Number(t.join("")) < Number(e.join(""));
        },
        bindChange: function(t) {
            var e = t.detail.value, r = this.data.newVal, a = {};
            if (e[1] !== r[1] || e[0] !== r[0] && 1 == +e[1]) {
                var n = this.getDate(e);
                a.days = this.getDays(n[0], n[1]);
            }
            a.newVal = this.isOverStartTime(e) ? this.start : e, this.setData(a);
        },
        onCancel: function() {
            this.setData({
                isShown: !1
            });
        },
        onReset: function() {
            this.triggerEvent("reset", {}, {}), this.onCancel();
        },
        onConfirm: function() {
            var t = this.getDate(this.data.newVal), e = this.joinDate(t);
            this.triggerEvent("confirm", {
                value: e
            }, {}), this.onCancel();
        }
    }
});